/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package devbureau.fstore.common.print.report;

import devbureau.fstore.common.ItemData;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import static devbureau.fstore.util.ExcelHelper.convert;
import static devbureau.fstore.util.Utilities.DateTime.formatShort;
import static devbureau.fstore.util.Utilities.Formatting.format;
import static devbureau.fstore.util.Utilities.Formatting.formatAsCurrency;

/**
 *
 * @author SSheypak
 */
public class PrintReminderReport {
    private static final Logger log = Logger.getLogger(PrintReminderReport.class);

    private List<ItemData> reminders;
    private OutputStream os;
    private static final String templateName = "reminders_template.xls";
    private static final int ROW_NUM = 6;

    private HSSFCellStyle leftAlign = null;
    private HSSFCellStyle rightAlign = null;

    public PrintReminderReport(List<ItemData> reminders, OutputStream os){
        this.reminders = reminders;
        this.os = os;
    }

    private void initStyles(HSSFWorkbook wb){
        leftAlign = wb.createCellStyle();
        leftAlign.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        rightAlign = wb.createCellStyle();
        rightAlign.setAlignment(HSSFCellStyle.ALIGN_RIGHT);

    }

    public void writeReport() throws Exception{
        log.debug("writeReport outStream["+os+"] billsSize["+reminders.size()+"]");
        InputStream xlsIS = null;
        try{
            xlsIS = this.getClass().getClassLoader().getResourceAsStream(templateName);
            HSSFWorkbook workBook = new HSSFWorkbook(xlsIS);
            initStyles(workBook);
            HSSFSheet sheet = workBook.getSheetAt(0);
            Double totalPrice = 0.0D;
            for(int i=0; i< reminders.size(); i++){
               ItemData item = reminders.get(i);
               HSSFRow row = sheet.createRow(ROW_NUM+i);
               
               row.createCell(0).setCellValue(convert(item.getSupplierLabel()));
               row.getCell(0).setCellStyle(leftAlign);
               
               row.createCell(1).setCellValue(convert(item.getName()));
               row.getCell(1).setCellStyle(leftAlign);
               
               row.createCell(2).setCellValue(convert(format(item.getLength())));
               row.getCell(2).setCellStyle(rightAlign);
               
               row.createCell(3).setCellValue(convert(formatShort(item.getDeliveryDate())));
               row.getCell(3).setCellStyle(rightAlign);
               
               row.createCell(4).setCellValue(convert(item.getQuantity().toString()));
               row.getCell(4).setCellStyle(rightAlign);
               
               row.createCell(5).setCellValue(convert(formatAsCurrency(item.getPrice())));
               row.getCell(5).setCellStyle(rightAlign);
               
               row.createCell(6).setCellValue(convert(formatAsCurrency(item.getPriceRUR())));
               row.getCell(6).setCellStyle(rightAlign);
               totalPrice += (item.getPriceRUR()*item.getBalance());
               
               row.createCell(7).setCellValue(convert(item.getBalance().toString()));
               row.getCell(7).setCellStyle(rightAlign);
               
            }

            HSSFRow totalRow = sheet.createRow(ROW_NUM+reminders.size());
            totalRow.createCell(5).setCellValue(convert("Итого: "));

            totalRow.createCell(6).setCellValue(convert(formatAsCurrency(totalPrice)));
            totalRow.getCell(6).setCellStyle(rightAlign);

            workBook.write(os);
        }catch(Exception e){
            log.error("Error while creating reminder report",e);
            throw new Exception("Произошла ошибка при создании отчета по остаткам. Отчет не создан.");
        }finally{
            if(xlsIS!=null){
                xlsIS.close();
            }
        }
        
    }
}
